<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\youji_income_order\Entity\YoujiIncomeOrder;
use Drupal\youji_income_order\Entity\YoujiIncomeOrderLine;
use EntityListWrapper;

/**
 * @OA\Schema(
 *   schema="IncomeOrderItem",
 *   type="object",
 *   description="IncomeOrderItem"
 * )
 */
class IncomeOrderItem extends AbstractBaseModel {

  protected function getEntityType() {
    return 'youji_income_order';
  }

  protected function setValue() {
    if ($this->entity instanceof YoujiIncomeOrder) {
      [$commission, $commissionUnlock] = $this->getCommission($this->entity);
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="user",
         *   ref="#/components/schemas/User",
         * ),
         */
        'user' => User::get($this->wrapper->order->uid->value()),
        /**
         * @OA\Property(
         *   property="products",
         *   type="array",
         *   description="产品列表",
         *   @OA\Items(ref="#/components/schemas/LineItemProduct")
         * ),
         */
        'products' => $this->getProducts($this->entity),
        /**
         * @OA\Property(
         *   property="orderId",
         *   type="integer",
         *   description="对应订单ID",
         * ),
         */
        'orderId' => (int) $this->entity->order_id,
        /**
         * @OA\Property(
         *   property="orderCreated",
         *   type="string",
         *   description="创建时间",
         * ),
         */
        'orderCreated' => $this->formatDate($this->entity->created, 'Y-m-d H:i:s'),
        //0 待结算 1 已结算 2 含有退款 3 全部退款
        /**
         * @OA\Property(
         *   property="status",
         *   type="integer",
         *   description="状态 0 待结算 1 已结算 2 含有退款 3 全部退款",
         * ),
         */
        'status' => (int) $this->entity->status,
        //状态标签
        /**
         * @OA\Property(
         *   property="statusLabel",
         *   type="string",
         *   description="状态标签",
         * ),
         */
        'statusLabel' => (string) $this->getStatusLabel($this->entity),
        //可获佣金
        /**
         * @OA\Property(
         *   property="commission",
         *   type="number",
         *   description="可获佣金",
         * ),
         */
        'commission' => $this->convertYuan($commission),
        /**
         * @OA\Property(
         *   property="commissionUnlock",
         *   type="number",
         *   description="已解锁佣金",
         * ),
         */
        'commissionUnlock' => $this->convertYuan($commissionUnlock),
        //佣金类型 0 直接 1 间接 2 团队
        /**
         * @OA\Property(
         *   property="commissionType",
         *   type="integer",
         *   description="佣金类型 0 直接 1 间接",
         * ),
         */
        'commissionType' => (int) $this->entity->commission_type,
        'commissionTypeLabel' => $this->commissionTypeLabel($this->entity->commission_type),
      ];
      $this->merge($value);
    }
  }

  protected function commissionTypeLabel(int $type) {
    switch ($type) {
      case 0:
        return '推广直推';
      case 1:
        return '推广间推';
      case 2:
        return '推广团推';
      case 3:
        return '店铺直推';
      case 4:
        return '店铺间推';
      case 5:
        return '店铺团推';
    }
    return '';
  }

  protected function getCommission(YoujiIncomeOrder $incomeOrder) {
    $commission = 0;
    $commissionUnlock = 0;
    $lines = youji_income_order_line_load_multiple(FALSE, [
      'income_order_id' => $incomeOrder->id,
    ]);
    foreach ($lines as $line) {
      if ($line instanceof YoujiIncomeOrderLine) {
        $commission += $line->commission;
        $commissionUnlock += $line->commission_unlock;
      }
    }
    return [
      $commission,
      $commissionUnlock,
    ];
  }

  protected function getProducts(YoujiIncomeOrder $order) {
    $products = [];
    $storeOrderWrapper = $order->wrapper();
    $lines = $storeOrderWrapper->order->lines;
    if ($lines instanceof EntityListWrapper) {
      foreach ($lines as $line) {
        if ($line->type->value() == 'product') {
          $products[] = LineItemProduct::get($line->id->value());
        }
      }
    }
    return $products;
  }

  protected function getStatusLabel(YoujiIncomeOrder $incomeOrder) {
    return isset($incomeOrder->getStatusOptions()[$incomeOrder->status]) ? $incomeOrder->getStatusOptions()[$incomeOrder->status] : "";
  }
}
